package com.example.springstudy.service.impl;

import com.example.springstudy.entity.SysUserDetails;
import com.example.springstudy.entity.User;
import com.example.springstudy.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User sysUser = userMapper.getUserByName(username);
        if (Objects.isNull(sysUser)) {
            throw new UsernameNotFoundException(username);
        }
        SysUserDetails userDetails = new SysUserDetails(sysUser);
        log.info("userDetails: {}", userDetails);
        return userDetails;
    }
}
